home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / tkern10.zip / GNUISH\GLOB.C < prev    next >
C/C++ Source or Header  |  1990-09-19  |  15KB  |  600 lines

  1. /* File-name wildcard pattern matching for GNU.
  2.    Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  19.    This port is distributed under the terms of the GNU General
  20.    Public License as published by the Free Software Foundation.
  21.  
  22.    $Header: e:/gnu/lib/RCS/glob.c 1.0.0.3 90/09/19 22:50:58 tho Exp $
  23.  */
  24.  
  25. /* To whomever it may concern: I have never seen the code which most
  26.    Unix programs use to perform this function.  I wrote this from scratch
  27.    based on specifications for the pattern matching.  --RMS.  */
  28.  
  29. #include <sys/types.h>
  30.  
  31. #if defined(USGr3) || defined(DIRENT) || defined(__GNU_LIBRARY__)
  32. #include <dirent.h>
  33. #define direct dirent
  34. #define D_NAMLEN(d) strlen((d)->d_name)
  35. #else /* not USGr3 or DIRENT or __GNU_LIBRARY__ */
  36. #define D_NAMLEN(d) ((d)->d_namlen)
  37. #ifdef USG
  38. #ifdef SYSNDIR
  39. #include <sys/ndir.h>
  40. #else
  41. #include "ndir.h"        /* Get ndir.h from the Emacs distribution.  */
  42. #endif /* not SYSNDIR */
  43. #else /* not USG */
  44. #include <sys/dir.h>
  45. #endif /* USG */
  46. #endif /* USGr3 */
  47.  
  48. #if    defined(STDC_HEADERS) || defined(__GNU_LIBRARY__)
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  52. #define index strchr
  53. #define rindex strrchr
  54. #else
  55.  
  56. #ifdef USG
  57. #include <string.h>
  58. #include <memory.h>
  59. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  60. #define index strchr
  61. #define rindex strrchr
  62. #else /* not USG */
  63. #include <strings.h>
  64. extern void bcopy ();
  65. #endif /* not USG */
  66.  
  67. extern char *malloc ();
  68. extern char *realloc ();
  69. extern void free ();
  70.  
  71. #ifndef NULL
  72. #define NULL 0
  73. #endif
  74. #endif    /* Not STDC_HEADERS or __GNU_LIBRARY__.  */
  75.  
  76. #ifdef __GNUC__
  77. #define alloca __builtin_alloca
  78. #else /* Not GCC.  */
  79. #ifdef sparc
  80. #include <alloca.h>
  81. #else /* Not sparc.  */
  82. #ifdef MSDOS
  83. #include <malloc.h>
  84. #else
  85. extern char *alloca ();
  86. #endif
  87. #endif /* sparc.  */
  88. #endif /* GCC.  */
  89.  
  90. #ifdef MSDOS
  91. #include <gnulib.h>
  92. static  int glob_match_after_star(char *,char *);
  93. static  char * *glob_dir_to_array(char *,char **);
  94. #endif /* MSDOS */
  95.   
  96.  
  97. /* Nonzero if '*' and '?' do not match an initial '.' for glob_filename.  */
  98. int noglob_dot_filenames = 1;
  99.  
  100. static int glob_match_after_star ();
  101.  
  102. /* Return nonzero if PATTERN has any special globbing chars in it.  */
  103.  
  104. int
  105. glob_pattern_p (pattern)
  106.      char *pattern;
  107. {
  108.   register char *p = pattern;
  109.   register char c;
  110.  
  111.   while ((c = *p++) != '\0')
  112.     switch (c)
  113.       {
  114.       case '?':
  115.       case '[':
  116.       case '*':
  117.     return 1;
  118.  
  119.       case '\\':
  120.     if (*p++ == '\0')
  121.       return 0;
  122.       }
  123.  
  124.   return 0;
  125. }
  126.  
  127.  
  128. /* Match the pattern PATTERN against the string TEXT;
  129.    return 1 if it matches, 0 otherwise.
  130.  
  131.    A match means the entire string TEXT is used up in matching.
  132.  
  133.    In the pattern string, `*' matches any sequence of characters,
  134.    `?' matches any character, [SET] matches any character in the specified set,
  135.    [!SET] matches any character not in the specified set.
  136.  
  137.    A set is composed of characters or ranges; a range looks like
  138.    character hyphen character (as in 0-9 or A-Z).
  139.    [0-9a-zA-Z_] is the set of characters allowed in C identifiers.
  140.    Any other character in the pattern must be matched exactly.
  141.  
  142.    To suppress the special syntactic significance of any of `[]*?!-\',
  143.    and match the character exactly, precede it with a `\'.
  144.  
  145.    If DOT_SPECIAL is nonzero,
  146.    `*' and `?' do not match `.' at the beginning of TEXT.  */
  147.  
  148. int
  149. glob_match (pattern, text, dot_special)
  150.      char *pattern, *text;
  151.      int dot_special;
  152. {
  153.   register char *p = pattern, *t = text;
  154.   register char c;
  155.  
  156.   while ((c = *p++) != '\0')
  157.     switch (c)
  158.       {
  159.       case '?':
  160.     if (*t == '\0' || (dot_special && t == text && *t == '.'))
  161.       return 0;
  162.     else
  163.       ++t;
  164.     break;
  165.  
  166.       case '\\':
  167.     if (*p++ != *t++)
  168.       return 0;
  169.     break;
  170.  
  171.       case '*':
  172.     if (dot_special && t == text && *t == '.')
  173.       return 0;
  174.     return glob_match_after_star (p, t);
  175.  
  176.       case '[':
  177.     {
  178.       register char c1 = *t++;
  179.       int invert;
  180.  
  181.       if (c1 == '\0')
  182.         return 0;
  183.  
  184.       invert = (*p == '!');
  185.  
  186.       if (invert)
  187.         p++;
  188.  
  189.       c = *p++;
  190.       while (1)
  191.         {
  192.           register char cstart = c, cend = c;
  193.  
  194.           if (c == '\\')
  195.         {
  196.           cstart = *p++;
  197.           cend = cstart;
  198.         }
  199.  
  200.           if (cstart == '\0')
  201.         return 0;    /* Missing ']'. */
  202.  
  203.           c = *p++;
  204.  
  205.           if (c == '-')
  206.         {
  207.           cend = *p++;
  208.           if (cend == '\\')
  209.             cend = *p++;
  210.           if (cend == '\0')
  211.             return 0;
  212.           c = *p++;
  213.         }
  214.           if (c1 >= cstart && c1 <= cend)
  215.         goto match;
  216.           if (c == ']')
  217.         break;
  218.         }
  219.       if (!invert)
  220.         return 0;
  221.       break;
  222.  
  223.     match:
  224.       /* Skip the rest of the [...] construct that already matched.  */
  225.       while (c != ']')
  226.         {
  227.           if (c == '\0')
  228.         return 0;
  229.           c = *p++;
  230.           if (c == '\0')
  231.         return 0;
  232.           if (c == '\\')
  233.         p++;
  234.         }
  235.       if (invert)
  236.         return 0;
  237.       break;
  238.     }
  239.  
  240.       default:
  241.     if (c != *t++)
  242.       return 0;
  243.       }
  244.  
  245.   return *t == '\0';
  246. }
  247.  
  248. /* Like glob_match, but match PATTERN against any final segment of TEXT.  */
  249.  
  250. static int
  251. glob_match_after_star (pattern, text)
  252.      char *pattern, *text;
  253. {
  254.   register char *p = pattern, *t = text;
  255.   register char c, c1;
  256.  
  257.   while ((c = *p++) == '?' || c == '*')
  258.     if (c == '?' && *t++ == '\0')
  259.       return 0;
  260.  
  261.   if (c == '\0')
  262.     return 1;
  263.  
  264.   if (c == '\\')
  265.     c1 = *p;
  266.   else
  267.     c1 = c;
  268.  
  269.   --p;
  270.   while (1)
  271.     {
  272.       if ((c == '[' || *t == c1) && glob_match (p, t, 0))
  273.     return 1;
  274.       if (*t++ == '\0')
  275.     return 0;
  276.     }
  277. }
  278.  
  279. /* Return a vector of names of files in directory DIR
  280.    whose names match glob pattern PAT.
  281.    The names are not in any particular order.
  282.    Wildcards at the beginning of PAT do not match an initial period
  283.    if noglob_dot_filenames is nonzero.
  284.  
  285.    The vector is terminated by an element that is a null pointer.
  286.  
  287.    To free the space allocated, first free the vector's elements,
  288.    then free the vector.
  289.  
  290.    Return NULL if cannot get enough memory to hold the pointer
  291.    and the names.
  292.  
  293.    Return -1 if cannot access directory DIR.
  294.    Look in errno for more information.  */
  295.  
  296. char **
  297. glob_vector (pat, dir)
  298.      char *pat;
  299.      char *dir;
  300. {
  301.   struct globval
  302.   {
  303.     struct globval *next;
  304.     char *name;
  305.   };
  306.  
  307.   DIR *d;
  308.   register struct direct *dp;
  309.   struct globval *lastlink;
  310.   register struct globval *nextlink;
  311.   register char *nextname;
  312.   unsigned int count;
  313.   int lose;
  314.   register char **name_vector;
  315.   register unsigned int i;
  316.  
  317.   d = opendir (dir);
  318.   if (d == NULL)
  319.     return (char **) -1;
  320.  
  321.   lastlink = NULL;
  322.   count = 0;
  323.   lose = 0;
  324.  
  325.   /* Scan the directory, finding all names that match.
  326.      For each name that matches, allocate a struct globval
  327.      on the stack and store the name in it.
  328.      Chain those structs together; lastlink is the front of the chain.  */
  329.   while (1)
  330.     {
  331.       dp = readdir (d);
  332.       if (dp == NULL)
  333.     break;
  334. #ifdef MSDOS    /* dp->d_ino is always 0 in the MS-DOS implementation. */
  335.       if (glob_match (pat, dp->d_name, noglob_dot_filenames))
  336. #else
  337.       if (dp->d_ino != 0
  338.       && glob_match (pat, dp->d_name, noglob_dot_filenames))
  339. #endif /* MSDOS */
  340.     {
  341.       nextlink = (struct globval *) alloca (sizeof (struct globval));
  342.       nextlink->next = lastlink;
  343.       i = D_NAMLEN (dp) + 1;
  344.       nextname = (char *) malloc (i);
  345.       if (nextname == NULL)
  346.         {
  347.           lose = 1;
  348.           break;
  349.         }
  350.       lastlink = nextlink;
  351.       nextlink->name = nextname;
  352.       bcopy (dp->d_name, nextname, i);
  353.       count++;
  354.     }
  355.     }
  356.   closedir (d);
  357.  
  358.   if (!lose)
  359.     {
  360.       name_vector = (char **) malloc ((count + 1) * sizeof (char *));
  361.       lose |= name_vector == NULL;
  362.     }
  363.  
  364.   /* Have we run out of memory?  */
  365.   if (lose)
  366.     {
  367.       /* Here free the strings we have got.  */
  368.       while (lastlink)
  369.     {
  370.       free (lastlink->name);
  371.       lastlink = lastlink->next;
  372.     }
  373.       return NULL;
  374.     }
  375.  
  376.   /* Copy the name pointers from the linked list into the vector.  */
  377.   for (i = 0; i < count; ++i)
  378.     {
  379.       name_vector[i] = lastlink->name;
  380.       lastlink = lastlink->next;
  381.     }
  382.  
  383.   name_vector[count] = NULL;
  384.   return name_vector;
  385. }
  386.  
  387. /* Return a new array, replacing ARRAY, which is the concatenation
  388.    of each string in ARRAY to DIR.
  389.    Return NULL if out of memory.  */
  390.  
  391. static char **
  392. glob_dir_to_array (dir, array)
  393.      char *dir, **array;
  394. {
  395.   register unsigned int i, l;
  396.   int add_slash = 0;
  397.   char **result;
  398.  
  399.   l = strlen (dir);
  400.   if (l == 0)
  401.     return array;
  402.  
  403.   if (dir[l - 1] != '/')
  404.     add_slash++;
  405.  
  406.   for (i = 0; array[i] != NULL; i++)
  407.     ;
  408.  
  409.   result = (char **) malloc ((i + 1) * sizeof (char *));
  410.   if (result == NULL)
  411.     return NULL;
  412.  
  413.   for (i = 0; array[i] != NULL; i++)
  414.     {
  415.       result[i] = (char *) malloc (1 + l + add_slash + strlen (array[i]));
  416.       if (result[i] == NULL)
  417.     return NULL;
  418.       strcpy (result[i], dir);
  419.       if (add_slash)
  420.     result[i][l] = '/';
  421.       strcpy (result[i] + l + add_slash, array[i]);
  422.     }
  423.   result[i] = NULL;
  424.  
  425.   /* Free the input array.  */
  426.   for (i = 0; array[i] != NULL; i++)
  427.     free (array[i]);
  428.   free ((char *) array);
  429.   return result;
  430. }
  431.  
  432. /* Do globbing on PATHNAME.  Return an array of pathnames that match,
  433.    marking the end of the array with a null-pointer as an element.
  434.    If no pathnames match, then the array is empty (first element is null).
  435.    If there isn't enough memory, then return NULL.
  436.    If a file system error occurs, return -1; `errno' has the error code.
  437.  
  438.    Wildcards at the beginning of PAT, or following a slash,
  439.    do not match an initial period if noglob_dot_filenames is nonzero.  */
  440.  
  441. char **
  442. glob_filename (pathname)
  443.      char *pathname;
  444. {
  445.   char **result;
  446.   unsigned int result_size;
  447.   char *directory_name, *filename;
  448.   unsigned int directory_len;
  449.  
  450.   result = (char **) malloc (sizeof (char *));
  451.   result_size = 1;
  452.   if (result == NULL)
  453.     return NULL;
  454.  
  455.   result[0] = NULL;
  456.  
  457.   /* Find the filename.  */
  458.   filename = rindex (pathname, '/');
  459.   if (filename == NULL)
  460.     {
  461.       filename = pathname;
  462.       directory_name = "";
  463.       directory_len = 0;
  464.     }
  465.   else
  466.     {
  467.       directory_len = (filename - pathname) + 1;
  468.       directory_name = (char *) alloca (directory_len + 1);
  469.       bcopy (pathname, directory_name, directory_len);
  470.       directory_name[directory_len] = '\0';
  471.       ++filename;
  472.     }
  473.  
  474.   /* If directory_name contains globbing characters, then we
  475.      have to expand the previous levels.  Just recurse. */
  476.   if (glob_pattern_p (directory_name))
  477.     {
  478.       char **directories;
  479.       register unsigned int i;
  480.  
  481.       if (directory_name[directory_len - 1] == '/')
  482.     directory_name[directory_len - 1] = '\0';
  483.  
  484.       directories = glob_filename (directory_name);
  485.       if (directories == NULL)
  486.     goto memory_error;
  487.       else if (directories == (char **) -1)
  488.     return (char **) -1;
  489.       else if (*directories == NULL)
  490.     {
  491.       free ((char *) directories);
  492.       return (char **) -1;
  493.     }
  494.  
  495.       /* We have successfully globbed the preceding directory name.
  496.      For each name in DIRECTORIES, call glob_vector on it and
  497.      FILENAME.  Concatenate the results together.  */
  498.       for (i = 0; directories[i] != NULL; i++)
  499.     {
  500.       char **temp_results = glob_vector (filename, directories[i]);
  501.       if (temp_results == NULL)
  502.         goto memory_error;
  503.       else if (temp_results == (char **) -1)
  504.         /* This filename is probably not a directory.  Ignore it.  */
  505.         ;
  506.       else
  507.         {
  508.           char **array = glob_dir_to_array (directories[i], temp_results);
  509.           register unsigned int l;
  510.  
  511.           l = 0;
  512.           while (array[l] != NULL)
  513.         ++l;
  514.  
  515.           result = (char **) realloc (result,
  516.                       (result_size + l) * sizeof (char *));
  517.           if (result == NULL)
  518.         goto memory_error;
  519.  
  520.           for (l = 0; array[l] != NULL; ++l)
  521.         result[result_size++ - 1] = array[l];
  522.           result[result_size - 1] = NULL;
  523.           free ((char *) array);
  524.         }
  525.     }
  526.       /* Free the directories.  */
  527.       for (i = 0; directories[i] != NULL; i++)
  528.     free (directories[i]);
  529.       free ((char *) directories);
  530.  
  531.       return result;
  532.     }
  533.  
  534.   /* If there is only a directory name, return it. */
  535.   if (*filename == '\0')
  536.     {
  537.       result = (char **) realloc ((char *) result, 2 * sizeof (char *));
  538.       if (result == NULL)
  539.     return NULL;
  540.       result[0] = (char *) malloc (directory_len + 1);
  541.       if (result[0] == NULL)
  542.     goto memory_error;
  543.       bcopy (directory_name, result[0], directory_len + 1);
  544.       result[1] = NULL;
  545.       return result;
  546.     }
  547.   else
  548.     {
  549.       /* Otherwise, just return what glob_vector
  550.      returns appended to the directory name. */
  551.       char **temp_results = glob_vector (filename,
  552.                      (directory_len == 0
  553.                       ? "." : directory_name));
  554.  
  555.       if (temp_results == NULL || temp_results == (char **) -1)
  556.     return temp_results;
  557.  
  558.       return glob_dir_to_array (directory_name, temp_results);
  559.     }
  560.  
  561. memory_error:;
  562.   if (result != NULL)
  563.     {
  564.       register unsigned int i;
  565.       for (i = 0; result[i] != NULL; ++i)
  566.     free (result[i]);
  567.       free ((char *) result);
  568.     }
  569.   return NULL;
  570. }
  571.  
  572. #ifdef TEST
  573.  
  574. #ifdef MSDOS
  575. #include <stdio.h>
  576. #endif
  577.  
  578. main (argc, argv)
  579.      int argc;
  580.      char **argv;
  581. {
  582.   char **value;
  583.   int i, optind;
  584.  
  585.   for (optind = 1; optind < argc; optind++)
  586.     {
  587.       value = glob_filename (argv[optind]);
  588.       if (value == NULL)
  589.     puts ("virtual memory exhausted");
  590.       else if (value == (char **) -1)
  591.     perror (argv[optind]);
  592.       else
  593.     for (i = 0; value[i] != NULL; i++)
  594.       puts (value[i]);
  595.     }
  596.   exit (0);
  597. }
  598.  
  599. #endif /* TEST */
  600.